﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Utilities;
using System.Reflection;

namespace NameResolver
{

    [Serializable]
    public class NameResolverViewModel : INotifyPropertyChanged
	{
        #region Enums
        #endregion Enums
        
        #region Events
        #endregion Events
        
        #region Properties
        #region ClickOnceUpdater
        private ClickOnceExt clickOnceUpdater = new ClickOnceExt()
        {
            CheckUpdateInterval = Properties.Settings.Default.CheckUpdateInterval,
            IsRestartWhenUpdate = true,
            IsAutoUpdate = Properties.Settings.Default.IsAutoUpdate
        };
        public ClickOnceExt ClickOnceUpdater
        {
            get { return clickOnceUpdater; }
            private set
            {
                if (clickOnceUpdater == value) return;
                clickOnceUpdater = value;
                OnPropertyChanged("ClickOnceUpdater");
            }
        }
        #endregion

        #region IsAutoStartEnable
        public bool IsAutoStartEnable
        {
            get { return Properties.Settings.Default.IsAutoStartEnable; }
            set
            {
                Properties.Settings.Default.IsAutoStartEnable = value;
                CheckAutoStart();
                OnPropertyChanged("IsAutoStartEnable");
            }
        }
        #endregion

        #region IsAutoUpdate
        public bool IsAutoUpdate
        {
            get { return Properties.Settings.Default.IsAutoUpdate; }
            set
            {
                Properties.Settings.Default.IsAutoUpdate = value;
                ClickOnceUpdater.IsAutoUpdate = value;
                OnPropertyChanged("IsUpdateModeOn");
            }
        }
        #endregion

        #region CheckUpdateInterval
        public TimeSpan CheckUpdateInterval
        {
            get { return Properties.Settings.Default.CheckUpdateInterval; }
            set
            {
                Properties.Settings.Default.CheckUpdateInterval = value;
                ClickOnceUpdater.CheckUpdateInterval = value;
                OnPropertyChanged("CheckUpdateInterval");
            }
        }
        #endregion

        #region Messages
        private string strMessages;
        public string Messages
        {
            get { return strMessages; }
            set
            {
                if (strMessages == value) return;
                strMessages = value;
                OnPropertyChanged("Messages");
            }
        }
        #endregion

        #endregion Properties
        
        #region Methods
        public NameResolverViewModel()
        {
            CheckAutoStart();
        }

        public void AddLogMessage(string strMessage)
        {
            Messages += DateTime.Now.ToString() + "\t" + strMessage + "\n";
        }
        private void CheckAutoStart()
        {
            string path;
            if (ClickOnceUpdater.IsClickOnce) path = Properties.Settings.Default.StartPath;
            else path = Assembly.GetExecutingAssembly().Location;
            if (IsAutoStartEnable)
            {
                Utilities.AutoStartSettings.SetAutoStart(Properties.Settings.Default.ApplicationName, path);
            }
            else
                if (Utilities.AutoStartSettings.IsAutoStartEnabled(Properties.Settings.Default.ApplicationName, path))
                    Utilities.AutoStartSettings.UnSetAutoStart(Properties.Settings.Default.StartPath);
        }
        #endregion Methods
        
        #region Fields
        #endregion Fields
        
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged = delegate{};
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion INotifyPropertyChanged Members
	}
}
